<?php

namespace App\Http\Controllers\Admin\hwtc;

use App\Http\Requests;
use App\Models\hwtc\Protype;
use Illuminate\Http\Request;
use App\Http\Controllers\Admin\CommonController;
use App\Models\hwtc\Proversion,App\Models\hwtc\Product,App\Models\hwtc\Plan;
use App\Models\hwtc\Auto_product,App\Models\hwtc\Customer,App\User;

class VisualizationController extends CommonController
{
    public function __construct()
    {
        parent::__construct();
    }

    function statistic(){
        $data = [];
        $protypes=Protype::get();
        foreach($protypes as $protype){

            $proversion_ids=Proversion::where('protype_id',$protype->id)->lists('id');

            if(!(stripos($protype->protype,'DACA02')===false)){
                $amount=Auto_product::whereIn('proversion_id', $proversion_ids)->where('status','<>',0)->count();
            }else{
                $amount=Product::whereIn('proversion_id', $proversion_ids)->where('isdone',2)->count();
            }

            if($amount!=0){
                $data['protype'][]=/*$protype->proname.*/$protype->protype;
                $data['amount'][]=$amount;
            }
        }

        return $data;
    }

    function current_plan(){

        $admin = \Auth::user(); //先获取登录用户信息  不同登录用户，看到的执行订单不一样

        if($admin->hasRole('admin') || $admin->hasRole('root') || $admin->hasRole('watcher')){
            //说明是超级管理员 or 海微专员

            //查找当前执行订单
            $plans=Plan::where('status',2)->with('protype')->get();

        }else{
            $customer=Customer::where('user_id',$admin->id)->first(); //查找当前登录用户对应的customer
            if(!$customer){
                return redirect(route('system.user.index'))->with('error', '当前登录用户并未关联生产客户，请先设置！');
            }

            //查找当前登录客户下面的子用户
            $children_users=User::where('parent_id',$admin->id)->lists('id');

            //查找当前登录客户下面的子工厂ids list
            $children_customers_ids=Customer::whereIn('user_id',$children_users)->lists('id');
            $children_customers_ids[]=$customer->id;  //当前用户和子用户ids

            //查找归属当前用户 以及子用户的正在执行订单
            $plans=Plan::whereIn('customer_id',$children_customers_ids)->where('status',2)->with('protype')->get();

        }


        $data = [];
//        $plans=Plan::where('status',2)->with('protype')->get();

        foreach($plans as $plan){
            $data[$plan->id]['text']=@$plan->protype->protype.$plan->protype->proname;
            $data[$plan->id]['subtext']=@$plan->planname."（第{$plan->id}号订单，订单总数：{$plan->amount}）";

//            if($plan->protype->protype=='DACA02'){
            if(!(stripos($plan->protype->protype,'DACA02')===false)){
                $finish=Auto_product::where('planid',$plan->id)->where('status',2)->count();
                $assign=Auto_product::where('planid',$plan->id)->where('status',1)->count();
            }else{
                $finish=Product::where('planid',$plan->id)->where('isdone',2)->count();
                $assign=Product::where('planid',$plan->id)->where('isdone',1)->count();
            }

            $not_assign=$plan->amount-$finish-$assign;
            $data[$plan->id]['series'][]=['value'=>$finish,'name'=>'完成生产'];
            $data[$plan->id]['series'][]=['value'=>$assign,'name'=>'已分配号,未确认'];
            $data[$plan->id]['series'][]=['value'=>$not_assign,'name'=>'未生产'];
        }
        return $data;
    }



}
